package csnet.openflow.component.controller;

import java.io.IOException;

import csnet.openflow.component.link.AbstractLink;
import csnet.openflow.packet.model.Packet;
import csnet.openflow.util.fileOperation.StreamWriter;
import csnet.openflow.util.log.LoggerGenerator;

public class ConstantLatencyController extends AbstractLink implements
        Controller {
    private int latency;
    private StreamWriter writer;

    public ConstantLatencyController(int latency) {
        super(Category.LIST);
        this.latency = latency;
    }

    @Override
    protected void changePacket(Packet packet) {
        packet.statistic.timestamp += latency;
        recordPacket(packet);
    }

    public void setLogger(LoggerGenerator logger) {
        writer = logger.getBinaryLogger("controller");
    }

    private void recordPacket(Packet packet) {
        try {
            writer.writeLong(packet.statistic.packetIndex);
            writer.writeLong(packet.getTimestamp());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
